<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html lang="en">
<head>
		<base href="<%=basePath%>">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
		<meta charset="utf-8" />
		<title>货源维护</title>
		<meta name="description" content="" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />
		
		<link rel="stylesheet" href="static/ace/css/bootstrap.css" />
		<link rel="stylesheet" href="static/ace/css/font-awesome.css" />
		
		<!-- text fonts -->
		<link rel="stylesheet" href="static/ace/css/ace-fonts.css" />
		<!-- ace styles -->
		<link rel="stylesheet" href="static/ace/css/ace.css" class="ace-main-stylesheet" id="main-ace-style" />
		<script type="text/javascript" src="static/js/jquery-1.7.2.js" >
		<script type="text/javascript">
			$(top.hangge());
		</script>
		<script type="text/javascript">
		$(document).ready(function() {
			//单选复选
			var flag = true;
			$("#selectAll").click(function() {

				if (this.checked) {
					$("#fu:checkbox,selectAll").prop("checked", true);
				} else {
					$("#fu:checkbox,selectAll").prop("checked", false);
				}
			});

			$(".s1").click(function() {
				var obj = $(".s1");
				var flag = true;
				obj.each(function() {
					if (!$(this).is(":checked")) {
						flag = false;
						return false;
					}
				});
				if (flag == true) {
					$("#selectAll").prop("checked", true);
				} else {
					$("#selectAll").prop("checked", false);
				}

			});
		});
		
		</script>
</head>
<body class="no-skin" id="bodyId">

<div class="main-container" id="main-container">
	<div class="main-content">
		<div class="main-content-inner">
			<div class="page-content">
				<div class="row">
					<div class="col-xs-12">
					<form action="<%=path%>/common/index" method="post" name="goodsForm" id="goodsForm">
						<div id="test">
							<table style="width:100%;">
								<tr>
									<td style="vertical-align:top;">
										<a title="新增" class="btn btn-mini btn-info"
										   onclick="addRow()">
											<i class="ace-icon fa bigger-120">新增</i>
										</a>
									</td>

									<td style="vertical-align:top;">
										<a title="保存" class="btn btn-mini btn-info"style="right:350px;"
										   onclick="save();">
											<i class="ace-icon fa bigger-120">保存</i>
										</a>
									</td>
									<td style="vertical-align:top;">
										<a title="删除" class="btn btn-mini btn-info"style="right:701px;"
										   onclick="deleteInfo();">
											<i class="ace-icon fa bigger-120">删除</i>
										</a>
									</td>
								</tr>
							</table>
						
						
						<table id="simple-table" class="table table-striped table-bordered table-hover"  style="margin-top:5px;">
							
							<thead>
								<tr id="test1">
									<th class="center" style="width:35px;">
									<label class="pos-rel"><input type="checkbox" class="ace" id="selectAll" /><span class="lbl"></span></label>
									</th>
									
									<th class="center" style="width:50px;"id="option0">序号</th>
									<th class="center" id="option2"style="width:300px;">货源代码</th>
									<th class="center" id="option3"style="width:300px;">货源名称</th>
				 					<th class="center" id="option4"style="width:300px;">排列顺序</th>
									<th class="center" id="option5"style="width:300px;">是否可用</th>
								</tr>
							</thead>
							<tbody>
							<c:choose>
								<c:when test="${not empty list}">
									<c:forEach items="${list}" var="goods" varStatus="vs">		
										<tr id="trId">
										<td class="dbclicktd" style="width: 30px;">
												<label><input type='checkbox' value="${goods.id }"  name="ids" class="ace" id="ids"/><span class="lbl"></span></label>
												
											</td>
											<td class="dbclicktd"  id="td1">${goods.offset+vs.index+1}</td>
											<td class='dbclicktd' style="width: 30px;"id="td2">${goods.goods_type_code}</td>
											<td class="dbclicktd" id="td3">${goods.goods_type_name}</td>
											<td class="dbclicktd"id="td4">${goods.ord_num}</td>
											<c:if test="${goods.enable ==1}">
											<td class="dbclicktd"id="td5">是</td>
											</c:if>
											<c:if test="${goods.enable ==0}">
											<td class="dbclicktd" id="td5">否</td>
											</c:if>
											
										</tr>
										
									
									</c:forEach>

								</c:when>
							</c:choose>
							</tbody>
						</table>
						</div>
						</form>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>

</body>
<script>


	//失去焦点
$("#main-container").click(function(){
	 $("#tt1").blur(function(){
			$(this).closest("td").text($(this).val())
			
	    	});
	    $("#tt2").blur(function(){
			$(this).closest("td").text($(this).val())
	    	});
	    $("#tt3").blur(function(){
			$(this).closest("td").text($(this).val())
	    	});
	    $("#tt4").blur(function(){
			$(this).closest("td").text($(this).val())

	    	});
    });

var rowCount=$("#simple-table").find("tr").length-1;  //获取行数  
  
//添加行  
function addRow(){  
    rowCount++;  
    var newRow='<tr id="option'+rowCount+'"><td class="dbclicktd"><label class="pos-rel"><input type="checkbox"name="ids"/></label></td><td class="dbclicktd">'+rowCount+'</td><td class="oz-property" ><input type="text" id="tt1"  style="width:300px"></td><td class="dbclicktd" ><input type="text" id="tt2" style="width:300px"><td class="dbclicktd" ><input type="text"  id="tt3" style="width:300px"><td class="dbclicktd" ><input type="text" id="tt4"  style="width:100px"></td></td></td></tr>';  
    $('#simple-table').append(newRow); 

}
//暂时保存
$("#simple-table").find(".dbclicktd").bind("dblclick", function () {
    var input = "<input type='text' id='temp' style='width:320px;' value=" + $(this).text() + " >";
    $(this).text("");
    $(this).append(input);
    $("#temp").focus();
    $("#temp").blur(function () {
        if ($(this).val() == "") {
            $(this).remove();
        } else {
            $(this).closest("td").text($(this).val());
        }
    });
});
//保存
function save(){
var trList = [];
var $simpleTabletr = $("#simple-table tbody tr");
var list = {}
for(var i=0,len=$simpleTabletr.length;i<len;i++){
	var objTr = $simpleTabletr[i];
		trList.push(objTr);
}
//传到后台的data json格式
var data = [];
 for(var i=0,size=trList.length;i<size;i++){
	var $tds = $(trList[i]).find("td");
	//id
	var id = $($tds[0]).find("label").find("input[name='ids']")[0].value;
	//货源编码
	var goods_src_code = $($tds[2]).text();
	//货源名称
	var goods_src_name = $($tds[3]).text();
	//排列顺序
	var ord_num = $($tds[4]).text();
	//是否可用
	var enable = $($tds[5]).text(); 
	
	var item = {};
// 	item.你的实体字段名 = 具体的值;
	item.id = id.trim();
	item.goods_src_code = goods_src_code;
	item.goods_src_name = goods_src_name;
	item.ord_num = ord_num;
	if(enable=="是"){
		item.enable = "1"
	}else{
		item.enable = "0"
	}
	data.push(item)
}
 var dataStr = JSON.stringify(data);

 $.ajax({
             type: "POST",
             url: '<%=basePath%>goods/editMaintain',
             data: {dataJsonStr:dataStr},
             dataType: 'json',
             cache: false,
             success: function (data) {
            	 window.location.reload();
             }
         });

}
//删除
function deleteInfo(){
	var trList = [];
	var $simpleTabletr = $("#simple-table tbody tr");
	var list = {}
	for(var i=0,len=$simpleTabletr.length;i<len;i++){
		
		var objTr = $simpleTabletr[i];
		
		var flag = $(objTr).find("td").first().find("label").find("#ids")[0].checked;
		if(flag){
			trList.push(objTr);
		}
	}
	//传到后台的data json格式
	var data = [];
	 for(var i=0,size=trList.length;i<size;i++){
		var $tds = $(trList[i]).find("td");
		//id
		var id = $($tds[0]).find("label").find("input[name='ids']")[0].value;
		var item = {};
		item.id = id.trim();
		data.push(item)
	}
	 var dataStr = JSON.stringify(data);
	 $.ajax({
         type: "POST",
         url: '<%=basePath%>goods/deleteInfo',
         data: {dataJsonStr:dataStr},
         dataType: 'json',
         cache: false,
         success: function (data) {
        	 window.location.reload();
        	 
         }
     });
}

</script>
</html>